﻿/*
	Copyright (C) 2008  Ajay Handa - jmdcms.com 
	This program is free software; 
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	License for more details.
	Removing/Altering this copyright message is a violation of license terms.
*/

/*
	*** D O C U M E N T A T I O N ***
	---------------------------------
    Author: Ajay Handa
 *  Purpose: Add/Edit/Delete/Fetch JmdSite Entity
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;


namespace JMDCMS.BusinessObjects
{
    [Serializable()]
    public class JmdSite : BusinessBase<JmdSite>
    {

        #region  Business Methods

        private byte[] mTimestamp = new byte[8];

        private static PropertyInfo<Guid> IdProperty = new PropertyInfo<Guid>("Id");
        [System.ComponentModel.DataObjectField(true, true)]
        public Guid Id
        {
            get
            {
                return GetProperty<Guid>(IdProperty);
            }
        }

        private static PropertyInfo<System.Nullable<System.Guid>> ParentSiteIdProperty =
            new PropertyInfo<System.Nullable<System.Guid>>("ParentSiteId","Parent Site ID");
        public System.Nullable<System.Guid> ParentSiteId
        {
            get
            {
                return GetProperty<System.Nullable<System.Guid>>(ParentSiteIdProperty);
            }
            set
            {
                SetProperty<System.Nullable<System.Guid>>(ParentSiteIdProperty, value);
            }
        }

        private static PropertyInfo<System.Nullable<System.Guid>> AliasSiteIdProperty = 
            new PropertyInfo<System.Nullable<System.Guid>>("AliasSiteId", "Alias Site ID");
        public System.Nullable<System.Guid> AliasSiteId
        {
            get
            {
                return GetProperty<System.Nullable<System.Guid>>(AliasSiteIdProperty);
            }
            set
            {
                SetProperty<System.Nullable<System.Guid>>(AliasSiteIdProperty, value);
            }
        }

        private static PropertyInfo<string> SiteURLProperty =
            new PropertyInfo<string>("SiteURL", "Site URL");
        public string SiteURL
        {
            get
            {
                return GetProperty<string>(SiteURLProperty);
            }
            set
            {
                SetProperty<string>(SiteURLProperty, value);
            }
        }

        private static PropertyInfo<string> SiteFolderProperty =
            new PropertyInfo<string>("SiteFolder", "Site Folder");
        public string SiteFolder
        {
            get
            {
                return GetProperty<string>(SiteFolderProperty);
            }
            set
            {
                SetProperty<string>(SiteFolderProperty, value);
            }
        }

        public override string ToString()
        {
            return Id.ToString();
        }

        #endregion

        #region  Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // add AuthorizationRules here
        }

        public static bool CanAddObject()
        {
            return true;
        }

        public static bool CanGetObject()
        {
            return true;
        }

        public static bool CanDeleteObject()
        {
            return true;
        }

        public static bool CanEditObject()
        {
            return true;
        }

        #endregion

        #region  Factory Methods

        public static JmdSite NewJmdSite()
        {
          if (!(CanAddObject()))
            throw new System.Security.SecurityException("User not authorized to add a site");
          return DataPortal.Create<JmdSite>();
        }

        public static JmdSite GetJmdSite(Guid id)
        {
          if (!(CanGetObject()))
            throw new System.Security.SecurityException("User not authorized to view a site");
          return DataPortal.Fetch<JmdSite>(new SingleCriteria<JmdSite, Guid>(id));
        }

        public static void DeleteJmdSite(Guid id)
        {
          if (!(CanDeleteObject()))
            throw new System.Security.SecurityException("User not authorized to remove a site");
          DataPortal.Delete(new SingleCriteria<JmdSite, Guid>(id));
        }

        public override JmdSite Save()
        {
          if (IsDeleted && !(CanDeleteObject()))
            throw new System.Security.SecurityException("User not authorized to remove a site");
          else if (IsNew && !(CanAddObject()))
            throw new System.Security.SecurityException("User not authorized to add a site");
          else if (!(CanEditObject()))
            throw new System.Security.SecurityException("User not authorized to update a site");
          return base.Save();
        }

        private JmdSite()
        { /* require use of factory methods */ }

        #endregion

        #region  Data Access

        [RunLocal()]
        protected override void DataPortal_Create()
        {
            LoadProperty<Guid>(IdProperty, Guid.NewGuid());
            ValidationRules.CheckRules();
        }

        private void DataPortal_Fetch(SingleCriteria<JmdSite, Guid> criteria)
        {
            using (var ctx = ContextManager<JMDCMS.Linq.JMDCMSDataContext>.GetManager(Database.JmdCmsConnection))
            {
                // get JmdSite data
                var data = (from p in ctx.DataContext.JmdSites
                            where p.ID == criteria.Value
                            select p).Single();

                LoadProperty<Guid>(IdProperty, data.ID);
                LoadProperty<System.Nullable<System.Guid>>(AliasSiteIdProperty, data.AliasSiteID);
                LoadProperty<System.Nullable<System.Guid>>(ParentSiteIdProperty, data.ParentSiteID);
                LoadProperty<string>(SiteFolderProperty, data.SiteFolder);
                LoadProperty<string>(SiteURLProperty, data.SiteURL);

                mTimestamp = data.LastChanged.ToArray();
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            using (var ctx = ContextManager<JMDCMS.Linq.JMDCMSDataContext>.GetManager(Database.JmdCmsConnection))
            {
                // insert project data

                //System.Data.Linq.Binary lastChanged = null;
                //ctx.DataContext.addProject(
                //  ReadProperty<Guid>(IdProperty),
                //  ReadProperty<string>(NameProperty),
                //  ReadProperty<SmartDate>(StartedProperty),
                //  ReadProperty<SmartDate>(EndedProperty),
                //  ReadProperty<string>(DescriptionProperty),
                //  ref lastChanged);
                //mTimestamp = lastChanged.ToArray();
                //// update child objects
                //DataPortal.UpdateChild(ReadProperty<ProjectResources>(ResourcesProperty), this);
            }
        }


        #endregion

    }
}
